package 网络编程项目.login.utiles;


import 网络编程项目.login.entity.User;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class userUtil {
  public static User getUserByResultSet(ResultSet resultSet) {
    User user = new User();
    try {
      user.setId(resultSet.getInt("id"));
      user.setEmail(resultSet.getString("email"));
      user.setPassword(resultSet.getString("password"));
      user.setPhone(resultSet.getString("phone"));
      user.setState(resultSet.getBoolean("state"));
      user.setUserid(resultSet.getString("userid"));
      user.setUsername(resultSet.getString("username"));
      user.setImage(resultSet.getString("image"));
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return user;
  }

  //获取SQL的插入语句
  public static String getInsertSql(User user) {
    StringBuffer sql = new StringBuffer();
    Field[] fields = user.getClass().getDeclaredFields();
    Class<? extends User> userClass = user.getClass();
    StringBuffer str1 = new StringBuffer();
    StringBuffer str2 = new StringBuffer();
    for (Field field : fields) {
      try {
        String filedStr = field.getName();
        Object valueStr = userClass.getMethod("get" + ((char) (field.getName().charAt(0) + 'A' - 'a')) + field.getName().substring(1)).invoke(user);
        if (valueStr != null) {
          str1.append(filedStr + ",");
          if (valueStr instanceof Boolean) {
            boolean state = (boolean) valueStr;
            if (!state) {
              str2.append(0 + ",");
            } else {
              str2.append(1 + ",");
            }
          } else {
            str2.append("'" + valueStr + "',");
          }

        }
      } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
      }
    }
    sql.append("insert into user (")
        .append(str1.substring(0, str1.length() - 1))
        .append(")")
        .append(" values ")
        .append("(")
        .append(str2.substring(0, str2.length() - 1))
        .append(")");
    return sql.toString();
  }

  //获取那些属性不为空
  private static String getStr1(User user) {
    StringBuffer str1 = new StringBuffer();
    Class<? extends User> userClass = user.getClass();
    Field[] fields = userClass.getDeclaredFields();
    Method[] methods = userClass.getMethods();
    for (Field field : fields) {
      System.out.println();
    }
    return str1.toString();
  }

  //首字母大写
  private static String getFirstCharToUpper(String string) {
    return ((char) (string.charAt(0) + 'A' - 'a')) + string.substring(1);
  }

  //获取修改语句
  public static String getUpdateSql(User user) {
    StringBuffer str = new StringBuffer();
    user.setId(3);
    user.setEmail("123123@qq.com");
    Class<? extends User> userClass = user.getClass();
    Field[] fields = userClass.getDeclaredFields();
    StringBuffer str1 = new StringBuffer();
    for (Field field : fields) {
      try {
        String filedStr = field.getName();
        Object valueStr = userClass.getMethod("get" + ((char) (field.getName().charAt(0) + 'A' - 'a')) + field.getName().substring(1)).invoke(user);
        if (valueStr != null) {
          if (filedStr.equals("id")) {
            continue;
          }
          str1.append(filedStr + "='");
          if (valueStr instanceof Boolean) {
            boolean state = (boolean) valueStr;
            if (!state) {
              str1.delete(str1.length() - 1, str1.length());
              str1.append(0 + ",");
            } else {
              str1.delete(str1.length() - 1, str1.length());
              str1.append(1 + ",");
            }
          } else {
            str1.append(valueStr + "',");
          }

        }
      } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {
      }
    }
    str.append("update user set ")
        .append(str1.substring(0, str1.length() - 1))
        .append(" ")
        .append("where id=")
        .append(user.getId());
    return str.toString();
  }

  //删除用户
  public boolean deleteUserById(int id) {
    boolean execute = false;
    String sql = "delete from user where id=" + id;
    Connection connection = ConnectSingleton.getConnection();
    try {
      PreparedStatement statement = connection.prepareStatement(sql);
      execute = statement.execute();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return execute;
  }

}
